/*
 * Copyright (c) 2013 Acorn International.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http:www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.chinadrtv.batch.admin.service.Impl;

import com.chinadrtv.batch.admin.service.OzingCardBatchService;
import com.chinadrtv.batch.admin.util.Constants;
import com.chinadrtv.user.dao.ozing.OzingCardBatchDao;
import com.chinadrtv.user.model.OzingCardBatch;
import com.chinadrtv.user.model.User;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * User: Jack.Chen
 * Date: 13-1-10
 */
@Service("ozingCardBatchService")
public class OzingCardBatchServiceImpl implements OzingCardBatchService {

    @Autowired
    private OzingCardBatchDao ozingCardBatchDao;

    @Override
    public void update(OzingCardBatch batch) {
        ozingCardBatchDao.save(batch);
    }

    @Override
    public void lock(Long batchId, User user) {
        OzingCardBatch ozingCardBatch = ozingCardBatchDao.get(batchId);
        ozingCardBatch.setMakeCardLock(true);
        ozingCardBatch.setLockedBy(user.getUsername());
        ozingCardBatchDao.save(ozingCardBatch);
    }

    @Override
    public boolean unlock(Long batchId, User user) {
        OzingCardBatch ozingCardBatch = ozingCardBatchDao.get(batchId);
        if (ozingCardBatch.getLockedBy() != null && ozingCardBatch.getLockedBy().equals(user.getUsername())) {
            ozingCardBatch.setMakeCardLock(false);
            ozingCardBatch.setLockedBy(null);
            OzingCardBatch o = ozingCardBatchDao.save(ozingCardBatch);
            return true;
        }
        return false;
    }

    @Override
    public OzingCardBatch handleTriggerCardJob(JobParametersBuilder builder, User user) {

        JobParameters jobParameters = builder.toJobParameters();

        OzingCardBatch ozingCardBatch = new OzingCardBatch();
        ozingCardBatch.setCardType(jobParameters.getString("cardType"));
        ozingCardBatch.setCardQuantity(Integer.valueOf(jobParameters.getString("quantity")));
        ozingCardBatch.setCardParValue(jobParameters.getLong("cardValue"));
        ozingCardBatch.setCardSalePrice(jobParameters.getLong("cardSalePrice"));
        ozingCardBatch.setCardValidityPeriod(Integer.valueOf(jobParameters.getString("period")));
        ozingCardBatch.setPeriodType(jobParameters.getString("periodType"));
        ozingCardBatch.setCardActivationDeadline(jobParameters.getDate("deadline"));
        ozingCardBatch.setDescription(jobParameters.getString("description"));
        ozingCardBatch.setDateAdded(new Date());
        ozingCardBatch.setUpdateBy(user.getUsername());
        ozingCardBatch.setCreatedBy(user.getUsername());

        return ozingCardBatchDao.save(ozingCardBatch);
    }

    @Override
    public OzingCardBatch get(Long id) {
        return ozingCardBatchDao.get(id);
    }

    @Override
    public void updateBatchLog(User user, String content, Long  batchId) {
        ozingCardBatchDao.updateBatchLog(user,content,batchId);
    }

    @Override
    public List<OzingCardBatch> getAll() {
        return ozingCardBatchDao.getAll();
    }

    @Override
    public List<OzingCardBatch> getPaginatedList(int start_index, Integer num_per_page, Map<String, String> searchParaMap) {
        return ozingCardBatchDao.getPaginatedList(start_index, num_per_page, searchParaMap);
    }

    @Override
    public int count(Map<String, String> searchParaMap) {
        return ozingCardBatchDao.count(searchParaMap);
    }

    @Override
    public List<OzingCardBatch> getAllDistinct() {
        return ozingCardBatchDao.getAllDistinct();
    }

    @Override
    public List<OzingCardBatch> getAllBatchs() {
        return ozingCardBatchDao.getAllBatchs();
    }
}
